package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import model.Database;
import model.SearchContact;
import controller.ActionContactProp;
import controller.ActionHomeContact;
import controller.Engine;
import controller.GhostTextField;
import controller.SearchKeyListener;

@SuppressWarnings("serial")
public class HomeContact extends JPanel implements Observer{
	public static JPanel contact = new JPanel();
	static JPanel contactContainer = new JPanel();
	static JScrollPane scroll;
	static ActionListener action = new ActionHomeContact();
	static ActionListener actionContact = new ActionContactProp();
	static int contactCount = 2;
	static Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
	KeyListener key = new SearchKeyListener();
	JPanel scrollContainer;
	SearchContact c = new SearchContact();
	JPanel searchPanel;
	JLabel img = Engine.setImage("search.jpg", 47, 47);
	public static JTextField search = new JTextField("",23);
	public static String text="";
	Observer obs;

	public HomeContact() {
		contact = this;
		obs = this;
		search.setName("User");
		startHomeContact();
	}
	@SuppressWarnings("unused")
	public void startHomeContact(){
		if (SearchContact.cnt.countObservers()!=0)
			SearchContact.cnt.deleteObserver(obs);
		SearchContact.cnt.addObserver(this);
		if (search.getKeyListeners().length!=0)
			search.removeKeyListener(key);
		search.addKeyListener(key);
		GhostTextField focus = new GhostTextField(search,"CERCA");
		search.setName("cerca");
		contact.setBackground(Color.white);
		searchPanel = new JPanel();
		searchPanel.setLayout(new BoxLayout(searchPanel,BoxLayout.X_AXIS));
		searchPanel.setMinimumSize(new Dimension(340,38));
		searchPanel.setPreferredSize(new Dimension(340,38));
		searchPanel.setMaximumSize(new Dimension(340,38));
		searchPanel.setAlignmentX(CENTER_ALIGNMENT);
		searchPanel.setAlignmentY(CENTER_ALIGNMENT);
		searchPanel.setBackground(Color.white);
		searchPanel.add(Box.createHorizontalStrut(8));
		searchPanel.add(img);
		searchPanel.add(Box.createHorizontalStrut(2));
		searchPanel.add(search);

		contactContainer.setLayout(new BoxLayout(contactContainer,BoxLayout.Y_AXIS));
		contactContainer.setBackground(Color.white);
		scroll= new JScrollPane(contactContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setMinimumSize(new Dimension(340,340));
		scroll.setPreferredSize(new Dimension(340,340));
		scroll.setMaximumSize(new Dimension(340,340));
		scroll.setVisible(true);

		setSearchedContactList(text);

		scrollContainer = new JPanel();
		scrollContainer.add(Box.createHorizontalStrut(0));
		scrollContainer.add(scroll);
		scrollContainer.setBackground(Color.white);
		contact.setLayout(new BoxLayout(contact, BoxLayout.Y_AXIS));
		contact.add(Box.createVerticalStrut(12));

		contact.add(searchPanel);
		contact.add(Box.createVerticalStrut(6));
		contact.add(scrollContainer);
		contact.setMinimumSize(new Dimension(350,448));
		contact.setPreferredSize(new Dimension(350,448));
		contact.setMaximumSize(new Dimension(350,448));
		contact.setVisible(true);
		contact.validate();
	}

	public static void setSearchedContactList(String txt){
		SearchContact.cnt.Search(txt);
		contactContainer.removeAll();
		if (txt.isEmpty() || search.getForeground()==Color.lightGray){
			contactCount=0;
			contactContainer.removeAll();
			JButton descr = new JButton();
			descr.setPreferredSize(new Dimension(318,24));
			descr.add(new JLabel("Rubrica: "));
			descr.setFocusable(false);
			descr.setContentAreaFilled(false);
			contactContainer.add(descr);
			if (!Database.Utente.getRubrica().isEmpty())
				for (int i=0;i<Database.Utente.getRubrica().size();i++){
					JPanel contactPanel = new JPanel();
					contactPanel.setBackground(new Color(0,0,0,0));
					JButton contatto = new JButton();
					contatto.setMaximumSize(new Dimension(340,36));
					Component comp = new Component(){
					};
					comp.setName(""+Database.Utente.getRubrica().get(i).getID());
					contactPanel.setLayout(new BoxLayout(contactPanel,BoxLayout.X_AXIS));
					contactPanel.add(Engine.setImage(Database.Utente.getRubrica().get(i).getPhoto(),50,50));
					contactPanel.add(Box.createHorizontalStrut(5));
					contactPanel.add(new JLabel(Database.Utente.getRubrica().get(i).getFirstname()+" "+Database.Utente.getRubrica().get(i).getSecondname()));
					contactPanel.add(Box.createHorizontalGlue());
					contatto.add(comp);
					contatto.add(contactPanel);
					contatto.addActionListener(action);
					//contatto.setAlignmentX(LEFT_ALIGNMENT);
					contatto.setName("contatto");
					contactContainer.add(contatto);
				}
			contactCount = contactContainer.getComponentCount();
			if (contactCount==1){
				JButton contatto = new JButton();
				contatto.setPreferredSize(new Dimension(318,24));
				contatto.add(new JLabel("Non sono presenti contatti nella tua rubrica!"));
				contatto.setContentAreaFilled(false);
				contactContainer.add(contatto);
				contactCount = contactContainer.getComponentCount();
			}
		}
		else
		{
			boolean flag = true;
			Vector<JButton> contattiRubrica = new Vector<JButton>();
			Vector<JButton> contattiDatabase = new Vector<JButton>();
			for (int i=0;i<SearchContact.search.size();i++){
				JPanel contactPanel = new JPanel();
				contactPanel.setBackground(new Color(0,0,0,0));
				JButton contatto = new JButton();
				contatto.setMaximumSize(new Dimension(340,36));
				Component comp = new Component(){
				};
				comp.setName(""+SearchContact.search.get(i).getID());
				contatto.setName("contatto");
				contactPanel.setLayout(new BoxLayout(contactPanel,BoxLayout.X_AXIS));
				contactPanel.add(Engine.setImage(SearchContact.search.get(i).getPhoto(),50,50));
				contactPanel.add(Box.createHorizontalStrut(5));
				contactPanel.add(new JLabel(SearchContact.search.get(i).getFirstname()+" "+SearchContact.search.get(i).getSecondname()));
				contactPanel.add(Box.createHorizontalGlue());
				boolean flag1=false;
				for (int x=0;x<Database.Utente.getRubrica().size();x++)
					if (SearchContact.search.get(i).getID()==Database.Utente.getRubrica().get(x).getID()){
						flag1 = true;
						break;
					}
				if (!flag1){
					JButton addButton = new JButton();
					addButton.add(Engine.setImage("plus.png", 30, 30));
					addButton.setName("aggiungi_1");
					Component addComp = new Component(){
					};
					addComp.setName(""+SearchContact.search.get(i).getID());
					addButton.add(addComp);
					addButton.addActionListener(actionContact);
					addButton.setMargin(new Insets(0,-10,0,-10));
					addButton.setMaximumSize(new Dimension(36,36));
					addButton.setPreferredSize(new Dimension(36,36));
					contactPanel.add(addButton);
					flag1=false;
				}
				contatto.add(comp);
				contatto.add(contactPanel);
				contatto.addActionListener(action);
				for (int x=0;x<Database.Utente.getRubrica().size();x++)
					if (SearchContact.search.get(i).getID()==Database.Utente.getRubrica().get(x).getID()){
						contattiRubrica.add(contatto);
						flag = false;
						break;
					}
				if (flag)
					contattiDatabase.add(contatto);
				flag=true;
			}
			if (!contattiRubrica.isEmpty()){
				JButton contatto = new JButton();
				contatto.setPreferredSize(new Dimension(318,24));
				contatto.add(new JLabel("'"+txt+"' trovati nella rubrica: "));
				contatto.setContentAreaFilled(false);
				contactContainer.add(contatto);
				for (int i=0;i<contattiRubrica.size();i++)
					contactContainer.add(contattiRubrica.get(i));
			}
			if (!contattiDatabase.isEmpty()){
				JButton contatto = new JButton();
				contatto.setPreferredSize(new Dimension(318,24));
				contatto.add(new JLabel("'"+txt+"' trovati in LoAD: "));
				contatto.setContentAreaFilled(false);
				contactContainer.add(contatto);
				for (int i=0;i<contattiDatabase.size();i++)
					contactContainer.add(contattiDatabase.get(i));
			}
			contactCount = contactContainer.getComponentCount();
			if (contactCount==0){
				JButton contatto = new JButton();
				contatto.setPreferredSize(new Dimension(318,24));
				contatto.add(new JLabel("'"+txt+"' non ha prodotto risultati"));
				contatto.setContentAreaFilled(false);
				contactContainer.add(contatto);
				contactCount = contactContainer.getComponentCount();
			}
		}
		contactContainer.add(Box.createVerticalGlue());
	}

	public static void pauseSearch(){
		search.setEditable(false);
	}

	public static void restartSearch(){
		search.setEditable(true);
	}

	public static void uppdater() {
		SearchContact.cnt.updateForUser();
	}

	public static void restartHomeContact(){
		SearchContact.cnt.updateForUser();
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if ((Boolean)arg1 == false){
			contact.removeAll();
			startHomeContact();
			contact.repaint();
			contact.validate();
		}
		//System.out.println("xxxxxxxxxxxxxxxxxxxxx");
	}

}
